﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

using YEBHI.EF;
using YEBHI.Entities;
using System.Web.Security;
using YEBHI.Utilities;
using System.Data.SqlClient;
using System.Data;
namespace YEBHI.MVC.Controllers
{
    public class AccountController : Controller
    {
        YEBHIEntities db = new YEBHIEntities();
        [HttpGet]
        public ActionResult Login()
        {
            return View();
        }
        [HttpPost]
        public ActionResult Login(FormCollection ofrmLogin)
        {
            try
            {
                string strEmail = ofrmLogin["txtMoblogin"].Trim();
                string strPassword = ofrmLogin["txtMobPass"].Trim();
                DataTable dtLogin = EFUtils.ExecuteQueryTran("EXEC uspIA_USER_REGISTRATION_SELECT @IAUR_EMAIL='" + strEmail + "' , @IAUR_PASSWORD ='" + strPassword + "', @FLAG='Validate_User' ");
                if (dtLogin.Rows.Count > 0)
                {
                    string strIAUR_ID_ENC = YebhiUtil.Encrypt(dtLogin.Rows[0]["IAUR_ID"].ToString());
                    string strIAUR_EMAIL_ENC = YebhiUtil.Encrypt(dtLogin.Rows[0]["IAUR_EMAIL"].ToString());
                    HttpCookie cookUserLogin = new HttpCookie("Yebhi_Login");
                    cookUserLogin.Values.Add("uID", strIAUR_ID_ENC);
                    cookUserLogin.Values.Add("uEmail", strIAUR_EMAIL_ENC);
                    this.ControllerContext.HttpContext.Response.Cookies.Add(cookUserLogin);
                    return RedirectToActionPermanent("Account", "MyOrders");
                }
                return View();
            }
            catch (Exception ex)
            {

                ViewBag.Message = ex.Message;
                return View();
            }


        }
        [HttpGet]
        public ActionResult Registration()
        {
            IA_USER_REGISTRATION oIA_USER_REGISTRATION = new IA_USER_REGISTRATION();
            return View(oIA_USER_REGISTRATION);
        }
        [HttpPost]
        public ActionResult Registration(FormCollection ofrmRegistration)
        {
            try
            {
                IA_USER_REGISTRATION oclsUserProfile = new IA_USER_REGISTRATION();
                oclsUserProfile.IAUR_FNAME = ofrmRegistration["FirstName"];
                oclsUserProfile.IAUR_LNAME = ofrmRegistration["LastName"];
                oclsUserProfile.IAUR_EMAIL = ofrmRegistration["Email"];
                oclsUserProfile.IAUR_PHONE = ofrmRegistration["Phone"];
                oclsUserProfile.IAUR_COM_MOD = int.Parse(ofrmRegistration["CommunicationMode"]);
                oclsUserProfile.IAUR_COUNTRY_PHONE_CODE = "";
                oclsUserProfile.IAUR_UserImage = "";


                System.Data.Objects.ObjectResult<decimal?> result = db.uspIA_USER_REGISTRATION_INSERT(
                      oclsUserProfile.IAUR_FNAME
                      , oclsUserProfile.IAUR_LNAME
                      , oclsUserProfile.IAUR_PHONE
                      , oclsUserProfile.IAUR_COUNTRY_PHONE_CODE
                      , oclsUserProfile.IAUR_IS_RETAILER
                      , oclsUserProfile.IAUR_COM_MOD
                      , oclsUserProfile.IAUR_UserImage
                      , ofrmRegistration["Password"]
                      , oclsUserProfile.IAUR_EMAIL
                      , "INSERT"
                      );
                decimal? userid = result.FirstOrDefault();
                if (userid != null)
                {
                    ViewBag.Message = "Registration Done Successfully.";
                    return RedirectToAction("Registration");
                }
                return View(oclsUserProfile);
            }
            catch (Exception ex)
            {
                ViewBag.Message = ex.Message;
                IA_USER_REGISTRATION oclsUserProfile = new IA_USER_REGISTRATION();
                return View(oclsUserProfile);
            }

        }

        [HttpGet]
        public ActionResult MyOrders()
        {

            return View();
        }
    }
}
